﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Framework.AssetsManager;

public class StackableUI : MonoBehaviour
{
    class UIStackElement
    {
        public GameObject ui;
        public bool hideScene;
    }

    Stack<UIStackElement> stack = new Stack<UIStackElement>();
    #region load ui
    Dictionary<string, GameObject> _uiDictionary = new Dictionary<string, GameObject>();
    public GameObject this[string index]
    {
        get
        {
            if (_uiDictionary.ContainsKey(index) == false || _uiDictionary[index] == null)
            {
                GameObject ui = Instantiate(RM.Instance.GetResource("Prefab/UI/Panel/" + index), transform) as GameObject;
                ui.SetActive(false);
                _uiDictionary[index] = ui;
            }

            return _uiDictionary[index];
        }
    }
    #endregion

    public void PushStackUI(GameObject UI, bool hideScene = true)
    {
        if (hideScene)
        {
            SetHideScene();
        }

        if (stack.Count != 0)
        {
            stack.Peek().ui.SetActive(false);
        }

        UI.gameObject.SetActive(true);
        stack.Push(new UIStackElement() { ui = UI, hideScene = hideScene });
    }

    public void PopStackUI()
    {
        if (stack.Count == 0)
            return;

        var currentUI = stack.Pop();
        currentUI.ui.SetActive(false);

        if ((stack.Count > 0 && stack.Peek().hideScene == false) || stack.Count == 0)
        {
            SetShowScene(); 
        }
        else
        {
            SetHideScene();
        }

        if (stack.Count != 0)
        {
            stack.Peek().ui.SetActive(true);
        }
    }

    protected virtual void SetHideScene()
    {

    }

    protected virtual void SetShowScene()
    {

    }
}
